package com.example.websocket.service;

import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class UserServiceImp implements UserService{
    private static Map<String, WebSocketSession> sessionMap=new ConcurrentHashMap<>();

    @Override
    public Set<String> getSessionIds() {
        Set<String> ids = sessionMap.keySet();
        return ids;
    }

    @Override
    public WebSocketSession getSessionById(String id) {
        WebSocketSession session = sessionMap.get(id);
        return session;
    }

    @Override
    public void setSession(WebSocketSession session) {
        String key=session.getId();
        sessionMap.put(key,session);
    }

    @Override
    public void removeSession(WebSocketSession session) {
        String key=session.getId();
        sessionMap.remove(key);
    }

    @Override
    public Collection<WebSocketSession> getSessions() {
        return sessionMap.values();
    }
}
